package com.android.common.base.rv;

import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.UiThread;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;
import com.android.common.R;
import com.android.common.base.BaseFragment;
import com.android.common.mvp.model.CodeMsgBean;
import com.android.common.constant.Constant;
import com.android.common.mvp.view.ExceptionView;
import com.android.common.utils.AppUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * 列表基类
 * @param <T> 列表List中存放的数据类型
 */
public abstract class BaseRvFragment<T>
        extends BaseFragment<ViewDataBinding> implements BaseRvBindView.Listener{

    //当前页数
    protected int page = 1;

    public BaseRvBindView<T> mBaseRvBind;

    public int onSetRootViewId() {
        return R.layout.fragment_base_rv;
    }


    @Override
    protected void onInitView() {
        mBaseRvBind = new BaseRvBindView<>(mRootView);
        mBaseRvBind.initRvTopView(onCreateRvTopView());
        mBaseRvBind.initRvBottomView(onCreateRvBottomView());
        mBaseRvBind.setRvLayoutManager(onSetRvLayoutManager());
        mBaseRvBind.mSmartRefreshLayout.setEnableRefresh(onEnableRefresh());
        mBaseRvBind.mSmartRefreshLayout.setEnableLoadMore(onEnableLoadMore());
        mBaseRvBind.setListener(this);
        mBaseRvBind.setAdapter(initAdapter());
    }

    protected View onCreateRvTopView() {
        return null;
    }

    protected View onCreateRvBottomView() {
        return null;
    }

    @Override
    protected boolean onNeedSpecialErrorView() {
        return true;
    }

    @Override
    public void onErrorClick() {
        mErrorView.hide();
        mBaseRvBind.mSmartRefreshLayout.setVisibility(View.VISIBLE);
        if (onNeedAutoRefresh()) {
            if (!mBaseRvBind.mSmartRefreshLayout.autoRefresh()) {
                onPullRefresh();
            }
        } else {
            onLoadData();
        }
    }

    /**
     * 上拉加载的通用回调
     * 需要特殊处理时，重写该方法
     */
    @Override
    public void onPullLoadMore() {
        page++;
        onRefreshOrLoadMore();
    }

    /**
     * 下拉刷新的通用回调
     * 需要特殊处理时，重写该方法
     */
    @Override
    public void onPullRefresh() {
        page = 1;
        onRefreshOrLoadMore();
    }

    public LayoutManager onSetRvLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    protected void onRefreshOrLoadMore() {
    }

    protected abstract BaseRvAdapter<T> initAdapter();

    /**
     * 是否需要自动刷新
     */
    public boolean onNeedAutoRefresh() {
        return false;
    }
    /**
     * 是否允许下拉
     */
    @Override
    public boolean onEnableRefresh(){
        return true;
    }
    /**
     * 是否允许上拉
     */
    @Override
    public boolean onEnableLoadMore(){
        return true;
    }

    @Override
    public void onFailed(CodeMsgBean responseBean, String methodName) {
        super.onFailed(responseBean, methodName);
        refreshOrLoadFailed();
    }

    protected void setRVBackgroundColor(@ColorInt int color){
        mBaseRvBind.setRVBackgroundColor(color);
    }


    /**
     * 上拉加载或下拉刷新成功后的回调
     */
    @UiThread
    public void refreshOrLoadSuccess(List<T> list) {
        if (list == null) list = new ArrayList<>();
        refreshOrLoadSuccess(list, Constant.PAGE_SIZE);
    }

    /**
     * 上拉加载或下拉刷新成功后的回调
     */
    @UiThread
    public void refreshOrLoadSuccess(List<T> list, int total) {
        if (!isAlive() || mErrorView == null) return;
        mErrorView.setVisibility(View.GONE);
        if (AppUtils.isAlive(this)) {
            if (list == null) list = new ArrayList<>();
            if (mBaseRvBind.isRefresh()) {
                if (list.isEmpty()) {
                    mErrorView.setStateAndShow(ExceptionView.NO_CONTENT);
                } else {
                    mErrorView.hide();
                }
            } else {
                if (list.isEmpty()) {
                    mErrorView.setStateAndShow(ExceptionView.NO_CONTENT);
                } else {
                    mErrorView.hide();
                }
            }
            mBaseRvBind.refreshOrLoadSuccess(list,total);
        }
    }

    @UiThread
    public void refreshOrLoadFailed() {
        if (!isAlive() || mErrorView == null) return;
        if (AppUtils.isAlive(this)) {
            if (mBaseRvBind.isRefresh() || !mBaseRvBind.isLoadMore()) {
                mErrorView.setVisibility(View.VISIBLE);
                mErrorView.setStateAndShow(ExceptionView.RELOAD);
            }else {
                page--;
            }
            mBaseRvBind.refreshOrLoadFailed();
        }
    }

    protected void updateItem(int position){
        mBaseRvBind.updateItem(position);
    }

    protected void setEnableRefresh(boolean enableRefresh){
        mBaseRvBind.mSmartRefreshLayout.setEnableRefresh(enableRefresh);
    }

    protected void setEnableLoadMore(boolean enableLoadMore){
        mBaseRvBind.mSmartRefreshLayout.setEnableLoadMore(enableLoadMore);
    }
}
